<?php
App::uses ( 'AppController', 'Controller' );
/**
 * Clients Controller
 *
 * @property Client $Client
 */
class ClientsController extends AppController {
	var $components = array (
			'RequestHandler',
			'Session',
			'Cookie' 
	);
	/**
	 * search function starts
	 */
	public function search() {
		// $this->set('results',$this->Customer->search($this->data['Customer']['q']));
		if (isset ( $_REQUEST ["term"] )) {
			$keyword = h ( $_REQUEST ["term"] );
		} else {
			$keyword = $this->request->data ['Client'] ['keyword'];
		}
		// get keyword from querystring
		// used simpme or condition with singe value checking
		// replace ModelName with actual name of your Appmodel
		
		// $this->Session->setFlash($keyword);
		$search_key = "%$keyword%";
		$conditions = array (
				'OR' => array (
						'Client.id LIKE' => $search_key,
						'Client.surname LIKE' => $search_key,
						'Client.firstname LIKE' => $search_key 
				) 
		);
		
		$this->set ( 'isFound', $this->Client->find ( 'count', array (
				'conditions' => $conditions 
		) ) );
		
		$this->set ( 'list', $this->Client->find ( 'all', array (
				'conditions' => $conditions,
				"order" => array (
						"Client.surname" => "ASC" 
				) 
		) ) );
	}
	
	/**
	 * end of search function
	 */
	
	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
		$this->Client->recursive = 0;
		if ($this->request->is ( "ajax" )) {
			$this->layout = null;
		}
		$keyword="";
		if ($this->request->is ( "post" )) {
			$keyword = $this->request->data ['Client'] ['keyword'];
		}
		$conditions = array ();
		if (! empty ( $keyword )) {
			$search_key = "%$keyword%";
			$conditions = array (
					'OR' => array (
							'Client.id LIKE' => $search_key,
							'Client.surname LIKE' => $search_key,
							'Client.firstname LIKE' => $search_key 
					) 
			);
		
		}
		$this->set ( 'clients', $this->paginate ( "Client", $conditions ) );
	}
	
	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id        	
	 * @return void
	 */
	public function view($id = null) {
		if (! $this->Client->exists ( $id )) {
			throw new NotFoundException ( __ ( 'Invalid client' ) );
		}
		$options = array (
				'conditions' => array (
						'Client.' . $this->Client->primaryKey => $id 
				) 
		);
		$this->set ( 'client', $this->Client->find ( 'first', $options ) );
	}
	
	/**
	 * add method
	 *
	 * @return void
	 */
	public function add() {
		if ($this->request->is ( 'post' )) {
			$this->Client->create ();
			if ($this->Client->save ( $this->request->data )) {
				$this->Session->setFlash ( __ ( 'The client has been saved' ) );
				$this->redirect ( array (
						'action' => 'index' 
				) );
			} else {
				$this->Session->setFlash ( __ ( 'The client could not be saved. Please, try again.' ) );
			}
		}
	}
	
	/**
	 * edit method
	 *
	 * @throws NotFoundException
	 * @param string $id        	
	 * @return void
	 */
	public function edit($id = null) {
		if (! $this->Client->exists ( $id )) {
			throw new NotFoundException ( __ ( 'Invalid client' ) );
		}
		if ($this->request->is ( 'post' ) || $this->request->is ( 'put' )) {
			if ($this->Client->save ( $this->request->data )) {
				$this->Session->setFlash ( __ ( 'The client has been saved' ) );
				$this->redirect ( array (
						'action' => 'index' 
				) );
			} else {
				$this->Session->setFlash ( __ ( 'The client could not be saved. Please, try again.' ) );
			}
		} else {
			$options = array (
					'conditions' => array (
							'Client.' . $this->Client->primaryKey => $id 
					) 
			);
			$this->request->data = $this->Client->find ( 'first', $options );
		}
	}
	
	/**
	 * delete method
	 *
	 * @throws NotFoundException
	 * @throws MethodNotAllowedException
	 * @param string $id        	
	 * @return void
	 */
	public function delete($id = null) {
		$this->Client->id = $id;
		if (! $this->Client->exists ()) {
			throw new NotFoundException ( __ ( 'Invalid client' ) );
		}
		$this->request->onlyAllow ( 'post', 'delete' );
		if ($this->Client->delete ()) {
			$this->Session->setFlash ( __ ( 'Client ' . $id . ' deleted' ) );
			$this->redirect ( array (
					'action' => 'index' 
			) );
		}
		$this->Session->setFlash ( __ ( 'Client was not deleted' ) );
		$this->redirect ( array (
				'action' => 'index' 
		) );
	}

}
?>